from django.shortcuts import render
from rest_framework import filters
from rest_framework.generics import ListAPIView
from . import serializers
from goods.models import SKU
from drf_haystack.viewsets import HaystackViewSet
from .serializers import SKUIndexSerializer

class HotSkuListView(ListAPIView):
    serializer_class = serializers.HotSkuListSerializer
    pagination_class = None
    def get_queryset(self):
        category_id = self.kwargs.get("category_id")
        skus =  SKU.objects.filter(category_id=category_id).order_by("-sales")[:2]
        return skus

class SkuListView(ListAPIView):

    serializer_class = serializers.HotSkuListSerializer
    filter_backends = (filters.OrderingFilter,)
    def get_queryset(self):
        #1,获取分类编号
        category_id = self.kwargs.get("category_id")

        #2,根据分类编号查询数据
        skus = SKU.objects.filter(category_id=category_id)

        # 设置过滤后端,排序字段

        ordering_fields = ('create_time', 'price', "sales")
        return skus

class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    #1,数据集
    index_models = [SKU]

    #2,序列化器
    serializer_class = SKUIndexSerializer